package com.neusoft.EYSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.EYSystem.dao.ClientNursingProjectDAO;
import com.neusoft.EYSystem.dao.NursingProjectDAO;
import com.neusoft.EYSystem.dao.NursingRecordDAO;
import com.neusoft.EYSystem.dao.UserDAO;
import com.neusoft.EYSystem.po.*;
import com.neusoft.EYSystem.service.NursingRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class NursingRecordServiceImpl implements NursingRecordService {
    @Autowired
    private NursingRecordDAO nursingRecordDAO;
    @Autowired
    private NursingProjectDAO nursingProjectDAO;
    @Autowired
    private UserDAO userDAO;

    @Override
    public PageResponseBean<List<RecordAndProject>> pageNursingRecord(Long cur, Long number, NursingRecord nursingRecord, NursingProject searchProject) {
        // 1. 查询所有该客户的可见护理记录
        QueryWrapper<NursingRecord> wrapperRecord = new QueryWrapper<>();
        wrapperRecord.eq("client_id", nursingRecord.getClientId());
        wrapperRecord.eq("is_visible", 1);
        List<NursingRecord> allClientRecords = nursingRecordDAO.selectList(wrapperRecord);

        // 2. 构造完整 RecordAndProject 列表，支持护理项目名模糊过滤
        List<RecordAndProject> fullList = new ArrayList<>();
        for (NursingRecord record : allClientRecords) {
            // 查询护理项目
            NursingProject nursingProject = nursingProjectDAO.selectById(record.getProjectId());
            if (nursingProject == null) continue;

            // 模糊匹配护理项目名（如果有传入查询条件）
            if (searchProject != null && searchProject.getName() != null && !searchProject.getName().isEmpty()) {
                if (!nursingProject.getName().contains(searchProject.getName())) {
                    continue; // 不匹配，跳过
                }
            }

            // 查询护理员信息
            User caretaker = userDAO.selectById(record.getCaretakerId());

            // 组装数据
            RecordAndProject rap = new RecordAndProject();
            rap.setNursingRecord(record);
            rap.setNursingProject(nursingProject);
            rap.setUser(caretaker);
            fullList.add(rap);
        }

        fullList.sort(Comparator.comparing((RecordAndProject r) -> r.getNursingRecord().getNursingTime()).reversed());

        // 3. 手动分页
        int fromIndex = (int) ((cur - 1) * number);
        int toIndex = (int) Math.min(fromIndex + number, fullList.size());
        List<RecordAndProject> pageList = fromIndex < fullList.size() ? fullList.subList(fromIndex, toIndex) : new ArrayList<>();

        // 4. 返回分页数据
        PageResponseBean<List<RecordAndProject>> rb = new PageResponseBean<>(pageList);
        rb.setTotal(fullList.size());
        return rb;
    }



}
